package thefuture;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 *
 */
public class Future1 {

    public static void main(String[] args) throws InterruptedException {

        Runnable r = () -> System.out.println("task 1" + Thread.currentThread().getName());
        Runnable r2 = () -> System.out.println("task 2" + Thread.currentThread().getName());

        var executor = new ScheduledThreadPoolExecutor(2,
            new ThreadFactoryBuilder().setNameFormat("mul-executor-%d").setDaemon(false).build());

        ScheduledFuture<?> future = executor.scheduleAtFixedRate(r, 0, 1, TimeUnit.SECONDS);
        ScheduledFuture<?> future2 = executor.scheduleAtFixedRate(r2, 0, 1, TimeUnit.SECONDS);

        Thread.sleep(1000 * 8);

        System.out.println("cancel a task");
        future.cancel(true);

    }


}
